import {mode,play,screen,move} from "@/config/mode-config/config";
import {add, random} from "@/common/js/utils";

const state = {
    screenSize: screen.full,
    playing: play.pause,
    moving: move.stop,
    mode: mode.sequence,
    currentIndex: -1,
    playlist: [],
    dialogList: []
}

const getters = {
    screenSize: state => state.screenSize,
    play: state => state.playing,
    mode: state => state.mode,
    index: state => state.currentIndex,
    currentSong: state => state.playlist[state.currentIndex],
    playlist: state => state.playlist,
    dialogs: state => state.dialogList,
    moving: state => state.moving
}

const mutations = {
    setPlay: (state,flag) => {state.playing = flag},
    setSize: (state,flag) => {state.screenSize = flag},
    setMode: (state,mode) => {state.mode = mode},
    setIndex: (state,index) => {
        if(state.mode == mode.sequence || state.mode === mode.loop)
            state.currentIndex = index
        else
            state.currentIndex = random(0,state.playlist.length - 1)
    },
    setList: (state,list) => {state.playlist = list},
    setDialog: (state,list) => state.dialogList = list,
    setMoving: (state,flag) => state.moving = flag,
    addSong: (state,song) => add(state.dialogList,song),
    removeSong: (state,index) =>  state.dialogList.splice(index,1),
    clearSongs: state => state.dialogList = []
}

const actions = {
    select: ({commit},{index,list}) => {
        commit('setPlay',play.playing)
        commit('setSize',screen.full)
        commit('setMode',mode.sequence)
        commit('setIndex',index)
        commit('setList',list)
        commit('setDialog',list)
        commit('user/addRecent',list[index],{root:true})
    },
    random: ({commit},list) => {
        commit('setList',list)
        commit('setIndex',-1)
        commit('setMode',mode.random)
        commit('setDialog',list)
        commit('setPlay',play.playing)
        commit('setSize',screen.full)
    },
    single: ({commit},{index,song}) => {
        commit('addSong',song)
        commit('setIndex',index)
        commit('user/addRecent',song,{root: true})
    }
}

export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}
